import logging

from django.db import models

# 缩水，变形，毛边，缺料，烧焦，胶口流纹，胶口亮印，胶口银纹，结合线明显，料花，流痕
from django.utils import timezone

from api.pojie_A10.producer_actual_values_yqd_A10 import SampleProducer_actual_values_yqd_A10
from api.pojie_A10.producer_set_values_A10 import SampleProducer_set_A10
from api.serial.figure import FigureResult

logger = logging.getLogger('django')


class Label(models.Model):
    default_id = 1
    # 系统误判
    good_id = 2

    class Meta:
        verbose_name = '不良标签'
        verbose_name_plural = "不良标签"

    label_name = models.CharField(u'标签名称', max_length=64, null=False, blank=False)
    label_desc = models.CharField(u'标签描述', max_length=512, default='')

    @classmethod
    def get_default_label(cls):
        label = None
        try:
            label = Label.objects.get(pk=cls.default_id)
        except:
            logger.error("标签 [未指定] 被删除，程序无法运行")
        return label

    @classmethod
    def get_good_label(cls):
        label = None
        try:
            label = Label.objects.get(pk=cls.good_id)
        except:
            logger.error("标签 [品质正常] 被删除，程序无法运行")
        return label

    def __str__(self):
        return f'{self.pk, self.label_name}'


class Reason(models.Model):
    default_id = 1
    # 系统误判
    good_id = 2

    class Meta:
        verbose_name = '不良原因'
        verbose_name_plural = "不良原因"

    reason_name = models.CharField(u'不良原因', max_length=64, null=False, blank=False)
    reason_desc = models.CharField(u'不良描述', max_length=512, default='')

    @classmethod
    def get_default_reason(cls):
        return Reason.objects.filter(pk=cls.default_id)

    @classmethod
    def get_good_reason(cls):
        return Reason.objects.filter(pk=cls.good_id)

    def __str__(self):
        return f'{self.pk, self.reason_name}'


class Sample(models.Model):
    class Meta:
        verbose_name = '样本总表'
        verbose_name_plural = "样本总表"

    state_choice = (
        (0, '不良品'),
        (1, '未检测'),
        (2, '品质正常'),
        (3, '已反馈'),
        (4, '已删除'),
    )
    sample_id = models.IntegerField(u'模具产品id', default=0)
    state = models.SmallIntegerField(u'状态', choices=state_choice, default=0)
    mould = models.ForeignKey('Mould', on_delete=models.SET_NULL, null=True, blank=True, default=None)
    label = models.ForeignKey('Label', on_delete=models.SET_NULL, null=True, blank=True,
                              default=Label.default_id)
    reason = models.ForeignKey('Reason', on_delete=models.SET_NULL, null=True, blank=True,
                               default=Reason.default_id)
    times = models.FloatField(u'品质', default=0.0, null=True, blank=True)
    prod_time = models.DateTimeField(u'开始生产时间', default=timezone.now)
    period = models.IntegerField(null=True, default=0, blank=True)

    def __str__(self):
        if self.mould:
            mould_code = self.mould.mould_code
        else:
            mould_code = '模具已删除'
        return f'模具: {mould_code}, id: {self.sample_id}, 状态: {self.state_choice[self.state][1]}, 标签: {self.label.label_name}'


class NegFigure(models.Model):
    class Meta:
        verbose_name = '标签画像'
        verbose_name_plural = "标签画像"

    mould = models.ForeignKey('Mould', on_delete=models.DO_NOTHING, null=True, blank=True, default=None)
    label = models.ForeignKey('Label', on_delete=models.DO_NOTHING, null=True, blank=True, default=None)
    # figure = models.TextField(u'标签画像', default=None, null=True, blank=True)
    # count = models.IntegerField(u'该标签下负样本数量', default=0)
    figure_reason3 = models.TextField(u'该标签下原因为温度偏低的画像', default=None, null=True, blank=True)
    count_reason3 = models.IntegerField(u'该标签下原因为温度偏低负样本数量', default=0)

    figure_reason4 = models.TextField(u'该标签下原因为温度偏高的画像', default=None, null=True, blank=True)
    count_reason4 = models.IntegerField(u'该标签下原因为温度偏高负样本数量', default=0)

    figure_reason5 = models.TextField(u'该标签下原因为射速偏高的画像', default=None, null=True, blank=True)
    count_reason5 = models.IntegerField(u'该标签下原因为射速偏高负样本数量', default=0)

    figure_reason6 = models.TextField(u'该标签下原因为射速偏低的画像', default=None, null=True, blank=True)
    count_reason6 = models.IntegerField(u'该标签下原因为射速偏低负样本数量', default=0)

    figure_reason7 = models.TextField(u'该标签下原因为保压偏高的画像', default=None, null=True, blank=True)
    count_reason7 = models.IntegerField(u'该标签下原因为保压偏高负样本数量', default=0)

    figure_reason8 = models.TextField(u'该标签下原因为保压偏低的画像', default=None, null=True, blank=True)
    count_reason8 = models.IntegerField(u'该标签下原因为保压偏低负样本数量', default=0)

    figure_reason9 = models.TextField(u'该标签下原因为背压偏高的画像', default=None, null=True, blank=True)
    count_reason9 = models.IntegerField(u'该标签下原因为背压偏高负样本数量', default=0)

    figure_reason10 = models.TextField(u'该标签下原因为背压偏低的画像', default=None, null=True, blank=True)
    count_reason10 = models.IntegerField(u'该标签下原因为背压偏低负样本数量', default=0)

    figure_reason11 = models.TextField(u'该标签下原因为堵射嘴的画像', default=None, null=True, blank=True)
    count_reason11 = models.IntegerField(u'该标签下原因为堵射嘴负样本数量', default=0)

    figure_reason12 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason12 = models.IntegerField(u'', default=0)

    figure_reason13 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason13 = models.IntegerField(u'', default=0)

    figure_reason14 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason14 = models.IntegerField(u'', default=0)

    figure_reason15 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason15 = models.IntegerField(u'', default=0)

    figure_reason16 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason16 = models.IntegerField(u'', default=0)

    figure_reason17 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason17 = models.IntegerField(u'', default=0)

    figure_reason18 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason18 = models.IntegerField(u'', default=0)

    figure_reason19 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason19 = models.IntegerField(u'', default=0)

    figure_reason20 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason20 = models.IntegerField(u'', default=0)

    figure_reason21 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason21 = models.IntegerField(u'', default=0)

    figure_reason22 = models.TextField(u'', default=None, null=True, blank=True)
    count_reason22 = models.IntegerField(u'', default=0)

    @classmethod
    def get_figure_by_label(cls, mould, label):
        try:
            return cls.objects.get(mould=mould, label=label)
        except:
            return None

    def get_count(self, reason_id):
        if reason_id == 3 or reason_id == "3":
            return self.count_reason3
        elif reason_id == 4 or reason_id == "4":
            return self.count_reason4
        elif reason_id == 5 or reason_id == "5":
            return self.count_reason5
        elif reason_id == 6 or reason_id == "6":
            return self.count_reason6
        elif reason_id == 7 or reason_id == "7":
            return self.count_reason7
        elif reason_id == 8 or reason_id == "8":
            return self.count_reason8
        elif reason_id == 9 or reason_id == "9":
            return self.count_reason9
        elif reason_id == 10 or reason_id == "10":
            return self.count_reason10
        elif reason_id == 11 or reason_id == "11":
            return self.count_reason11
        elif reason_id == 12 or reason_id == "12":
            return self.count_reason12
        elif reason_id == 13 or reason_id == "13":
            return self.count_reason13
        elif reason_id == 14 or reason_id == "14":
            return self.count_reason14
        elif reason_id == 15 or reason_id == "15":
            return self.count_reason15
        elif reason_id == 16 or reason_id == "16":
            return self.count_reason16
        elif reason_id == 17 or reason_id == "17":
            return self.count_reason17
        elif reason_id == 18 or reason_id == "18":
            return self.count_reason18
        elif reason_id == 19 or reason_id == "19":
            return self.count_reason19
        elif reason_id == 20 or reason_id == "20":
            return self.count_reason20
        elif reason_id == 21 or reason_id == "21":
            return self.count_reason21
        elif reason_id == 22 or reason_id == "22":
            return self.count_reason22

    def set_count(self, reason_id, count):
        if reason_id == 3 or reason_id == "3":
            self.count_reason3 = count
            self.save()
            return self.count_reason3
        elif reason_id == 4 or reason_id == "4":
            self.count_reason4 = count
            self.save()
            return self.count_reason4
        elif reason_id == 5 or reason_id == "5":
            self.count_reason5 = count
            self.save()
            return self.count_reason5
        elif reason_id == 6 or reason_id == "6":
            self.count_reason6 = count
            self.save()
            return self.count_reason6
        elif reason_id == 7 or reason_id == "7":
            self.count_reason7 = count
            self.save()
            return self.count_reason7
        elif reason_id == 8 or reason_id == "8":
            self.count_reason8 = count
            self.save()
            return self.count_reason8
        elif reason_id == 9 or reason_id == "9":
            self.count_reason9 = count
            self.save()
            return self.count_reason9
        elif reason_id == 10 or reason_id == "10":
            self.count_reason10 = count
            self.save()
            return self.count_reason10
        elif reason_id == 11 or reason_id == "11":
            self.count_reason11 = count
            self.save()
            return self.count_reason11
        elif reason_id == 12 or reason_id == "12":
            self.count_reason12 = count
            self.save()
            return self.count_reason12
        elif reason_id == 13 or reason_id == "13":
            self.count_reason13 = count
            self.save()
            return self.count_reason13
        elif reason_id == 14 or reason_id == "14":
            self.count_reason14 = count
            self.save()
            return self.count_reason14
        elif reason_id == 15 or reason_id == "15":
            self.count_reason15 = count
            self.save()
            return self.count_reason15
        elif reason_id == 16 or reason_id == "16":
            self.count_reason16 = count
            self.save()
            return self.count_reason16
        elif reason_id == 17 or reason_id == "17":
            self.count_reason17 = count
            self.save()
            return self.count_reason11
        elif reason_id == 18 or reason_id == "18":
            self.count_reason18 = count
            self.save()
            return self.count_reason18
        elif reason_id == 19 or reason_id == "19":
            self.count_reason19 = count
            self.save()
            return self.count_reason19
        elif reason_id == 20 or reason_id == "20":
            self.count_reason20 = count
            self.save()
            return self.count_reason20
        elif reason_id == 21 or reason_id == "21":
            self.count_reason21 = count
            self.save()
            return self.count_reason21
        elif reason_id == 22 or reason_id == "22":
            self.count_reason22 = count
            self.save()
            return self.count_reason22

    def count_increment(self, reason_id):
        if reason_id == 3 or reason_id == "3":
            self.count_reason3 += 1
            self.save()
            return self.count_reason3
        elif reason_id == 4 or reason_id == "4":
            self.count_reason4 += 1
            self.save()
            return self.count_reason4
        elif reason_id == 5 or reason_id == "5":
            self.count_reason5 += 1
            self.save()
            return self.count_reason5
        elif reason_id == 6 or reason_id == "6":
            self.count_reason6 += 1
            self.save()
            return self.count_reason6
        elif reason_id == 7 or reason_id == "7":
            self.count_reason7 += 1
            self.save()
            return self.count_reason7
        elif reason_id == 8 or reason_id == "8":
            self.count_reason8 += 1
            self.save()
            return self.count_reason8
        elif reason_id == 9 or reason_id == "9":
            self.count_reason9 += 1
            self.save()
            return self.count_reason9
        elif reason_id == 10 or reason_id == "10":
            self.count_reason10 += 1
            self.save()
            return self.count_reason10
        elif reason_id == 11 or reason_id == "11":
            self.count_reason11 += 1
            self.save()
            return self.count_reason11
        elif reason_id == 12 or reason_id == "12":
            self.count_reason12 += 1
            self.save()
            return self.count_reason12
        elif reason_id == 13 or reason_id == "13":
            self.count_reason13 += 1
            self.save()
            return self.count_reason13
        elif reason_id == 14 or reason_id == "14":
            self.count_reason14 += 1
            self.save()
            return self.count_reason14
        elif reason_id == 15 or reason_id == "15":
            self.count_reason15 += 1
            self.save()
            return self.count_reason15
        elif reason_id == 16 or reason_id == "16":
            self.count_reason16 += 1
            self.save()
            return self.count_reason16
        elif reason_id == 17 or reason_id == "17":
            self.count_reason17 += 1
            self.save()
            return self.count_reason17
        elif reason_id == 18 or reason_id == "18":
            self.count_reason18 += 1
            self.save()
            return self.count_reason18
        elif reason_id == 19 or reason_id == "19":
            self.count_reason19 += 1
            self.save()
            return self.count_reason19
        elif reason_id == 20 or reason_id == "20":
            self.count_reason20 += 1
            self.save()
            return self.count_reason20
        elif reason_id == 21 or reason_id == "21":
            self.count_reason21 += 1
            self.save()
            return self.count_reason21
        elif reason_id == 22 or reason_id == "":
            self.count_reason22 += 1
            self.save()
            return self.count_reason22

    def count_reduce(self, reason_id):
        if reason_id == 3 or reason_id == "3":
            self.count_reason3 -= 1
            self.save()
            return self.count_reason3
        elif reason_id == 4 or reason_id == "4":
            self.count_reason4 -= 1
            self.save()
            return self.count_reason4
        elif reason_id == 5 or reason_id == "5":
            self.count_reason5 -= 1
            self.save()
            return self.count_reason5
        elif reason_id == 6 or reason_id == "6":
            self.count_reason6 -= 1
            self.save()
            return self.count_reason6
        elif reason_id == 7 or reason_id == "7":
            self.count_reason7 -= 1
            self.save()
            return self.count_reason7
        elif reason_id == 8 or reason_id == "8":
            self.count_reason8 -= 1
            self.save()
            return self.count_reason8
        elif reason_id == 9 or reason_id == "9":
            self.count_reason9 -= 1
            self.save()
            return self.count_reason9
        elif reason_id == 10 or reason_id == "10":
            self.count_reason10 -= 1
            self.save()
            return self.count_reason10
        elif reason_id == 11 or reason_id == "11":
            self.count_reason11 -= 1
            self.save()
            return self.count_reason11
        elif reason_id == 12 or reason_id == "12":
            self.count_reason12 -= 1
            self.save()
            return self.count_reason12
        elif reason_id == 13 or reason_id == "13":
            self.count_reason13 -= 1
            self.save()
            return self.count_reason13
        elif reason_id == 14 or reason_id == "14":
            self.count_reason14 -= 1
            self.save()
            return self.count_reason14
        elif reason_id == 15 or reason_id == "15":
            self.count_reason15 -= 1
            self.save()
            return self.count_reason15
        elif reason_id == 16 or reason_id == "16":
            self.count_reason16 -= 1
            self.save()
            return self.count_reason16
        elif reason_id == 17 or reason_id == "17":
            self.count_reason17 -= 1
            self.save()
            return self.count_reason17
        elif reason_id == 18 or reason_id == "18":
            self.count_reason18 -= 1
            self.save()
            return self.count_reason18
        elif reason_id == 19 or reason_id == "19":
            self.count_reason19 -= 1
            self.save()
            return self.count_reason19
        elif reason_id == 20 or reason_id == "20":
            self.count_reason20 -= 1
            self.save()
            return self.count_reason20
        elif reason_id == 21 or reason_id == "21":
            self.count_reason21 -= 1
            self.save()
            return self.count_reason21
        elif reason_id == 22 or reason_id == "22":
            self.count_reason22 -= 1
            self.save()
            return self.count_reason22

    def get_figure_by_reason(self, reason_id):
        if reason_id == 3 or reason_id == "3":
            if self.figure_reason3:
                figure = FigureResult(self.figure_reason3)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 4 or reason_id == "4":
            if self.figure_reason4:
                figure = FigureResult(self.figure_reason4)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 5 or reason_id == "5":
            if self.figure_reason3:
                figure = FigureResult(self.figure_reason5)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 6 or reason_id == "6":
            if self.figure_reason6:
                figure = FigureResult(self.figure_reason6)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 7 or reason_id == "7":
            if self.figure_reason7:
                figure = FigureResult(self.figure_reason7)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 8 or reason_id == "8":
            if self.figure_reason8:
                figure = FigureResult(self.figure_reason8)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 9 or reason_id == "9":
            if self.figure_reason9:
                figure = FigureResult(self.figure_reason9)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 10 or reason_id == "10":
            if self.figure_reason10:
                figure = FigureResult(self.figure_reason10)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 11 or reason_id == "11":
            if self.figure_reason11:
                figure = FigureResult(self.figure_reason11)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 12 or reason_id == "12":
            if self.figure_reason12:
                figure = FigureResult(self.figure_reason12)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 13 or reason_id == "13":
            if self.figure_reason13:
                figure = FigureResult(self.figure_reason13)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 14 or reason_id == "14":
            if self.figure_reason14:
                figure = FigureResult(self.figure_reason14)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 15 or reason_id == "15":
            if self.figure_reason15:
                figure = FigureResult(self.figure_reason15)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 16 or reason_id == "16":
            if self.figure_reason16:
                figure = FigureResult(self.figure_reason16)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 17 or reason_id == "17":
            if self.figure_reason17:
                figure = FigureResult(self.figure_reason17)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 18 or reason_id == "18":
            if self.figure_reason18:
                figure = FigureResult(self.figure_reason18)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 19 or reason_id == "19":
            if self.figure_reason19:
                figure = FigureResult(self.figure_reason19)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 20 or reason_id == "20":
            if self.figure_reason20:
                figure = FigureResult(self.figure_reason20)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 21 or reason_id == "21":
            if self.figure_reason21:
                figure = FigureResult(self.figure_reason21)
                return figure
            else:
                return FigureResult(None)
        elif reason_id == 22 or reason_id == "22":
            if self.figure_reason22:
                figure = FigureResult(self.figure_reason22)
                return figure
            else:
                return FigureResult(None)

    def save_figure_result(self, figure_str, reason_id):
        if reason_id == 3 or reason_id == "3":
            if self.figure_reason3 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason3 = figure_str
                self.save()
        elif reason_id == 4 or reason_id == "4":
            if self.figure_reason4 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason4 = figure_str
                self.save()
        elif reason_id == 5 or reason_id == "5":
            if self.figure_reason5 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason5 = figure_str
                self.save()
        elif reason_id == 6 or reason_id == "6":
            if self.figure_reason6 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason6 = figure_str
                self.save()
        elif reason_id == 7 or reason_id == "7":
            if self.figure_reason7 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason7 = figure_str
                self.save()
        elif reason_id == 8 or reason_id == "8":
            if self.figure_reason8 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason8 = figure_str
                self.save()
        elif reason_id == 9 or reason_id == "9":
            if self.figure_reason9 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason9 = figure_str
                self.save()
        elif reason_id == 10 or reason_id == "10":
            if self.figure_reason10 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason10 = figure_str
                self.save()
        elif reason_id == 11 or reason_id == "11":
            if self.figure_reason11 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason11 = figure_str
                self.save()
        elif reason_id == 12 or reason_id == "12":
            if self.figure_reason12 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason12 = figure_str
                self.save()
        elif reason_id == 13 or reason_id == "13":
            if self.figure_reason13 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason13 = figure_str
                self.save()
        elif reason_id == 14 or reason_id == "14":
            if self.figure_reason14 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason14 = figure_str
                self.save()
        elif reason_id == 15 or reason_id == "15":
            if self.figure_reason15 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason15 = figure_str
                self.save()
        elif reason_id == 16 or reason_id == "16":
            if self.figure_reason16 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason16 = figure_str
                self.save()
        elif reason_id == 17 or reason_id == "17":
            if self.figure_reason17 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason17 = figure_str
                self.save()
        elif reason_id == 18 or reason_id == "18":
            if self.figure_reason18 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason18 = figure_str
                self.save()
        elif reason_id == 19 or reason_id == "19":
            if self.figure_reason19 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason19 = figure_str
                self.save()
        elif reason_id == 20 or reason_id == "20":
            if self.figure_reason20 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason20 = figure_str
                self.save()
        elif reason_id == 21 or reason_id == "21":
            if self.figure_reason21 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason21 = figure_str
                self.save()
        elif reason_id == 22 or reason_id == "22":
            if self.figure_reason22 and not figure_str:
                logger.warning(f'{self} begin to update the figure, but new figure is null')
            if figure_str:
                self.figure_reason22 = figure_str
                self.save()

    def __str__(self):
        return f'模具: {self.mould.mould_code}, 标签:{self.label.label_name}'


class Mould(models.Model):
    class Meta:
        verbose_name = '机台模具'
        verbose_name_plural = "机台模具"

    default_gauss_times = 30.0
    state_choice = (
        (0, '未运行'),
        (1, '正在运行')
    )
    mould_code = models.CharField(u'模具编号', max_length=64, null=False, blank=False, unique=True)
    state = models.SmallIntegerField(u'工单状态,0表示未运行,1是正在运行', choices=state_choice, blank=False, null=False,
                                     default=0)
    sample_id = models.IntegerField(u'样本标识', null=False, blank=False, default=0)
    threshold = models.FloatField(u'阈值,标准差的倍数', null=False, blank=False, default=default_gauss_times)
    pos_figure = models.TextField(u'最近正样本画像', blank=True, null=True, default='')
    pos_count = models.IntegerField(u'正样本总数', blank=True, null=True, default=0)

    neg_count = models.IntegerField(u'负样本总数', blank=True, null=True, default=0)
    neg_figures = models.ManyToManyField(Label, related_name='neg_figures', through=NegFigure)
    samples = models.ManyToManyField(Label, related_name='samples', through=Sample)

    def get_positive(self):
        figure = FigureResult(self.pos_figure)
        return figure

    def save_positive(self, figure_str):
        self.pos_figure = figure_str
        self.save()

    def get_pos_count(self):
        return self.pos_count

    def get_neg_count(self):
        return self.neg_count

    def pos_increment(self):
        self.pos_count += 1
        self.save()
        return self.pos_count

    def pos_reduce(self):
        self.pos_count -= 1
        self.save()
        return self.pos_count

    def neg_increment(self):
        self.neg_count += 1
        self.save()
        return self.neg_count

    def neg_reduce(self):
        self.neg_count -= 1
        self.save()
        return self.neg_count

    def __str__(self):
        return f'{self.mould_code}, state:{Mould.state_choice[self.state]}, threshold:{self.threshold},' \
               f' sample_id:{self.sample_id}'


# 系统配置及上下文
class SysDic(models.Model):
    class Meta:
        verbose_name = '系统字典'
        verbose_name_plural = "系统字典"

    type = models.CharField(u'字典类型', max_length=32, null=False, blank=False)
    code = models.CharField(u'字典编码', max_length=32, null=False, blank=False)
    value = models.TextField(u'字典值', null=True, blank=True, default='')

    def __str__(self):
        return f'{self.type, self.code, self.value}'


def producer_yuqidong(SampleProducer_yqd):
    result = "无工单线程正在进行中！！！"
    a = SampleProducer_yqd.get_instance()

    if a is None:
        try:
            a = SampleProducer_yqd.new_instance()
            a.begin()
            result = "无工单启动！！！"
        except Exception as e:
            result = e
    # print(result)


def producer_yqd(producer):
    result = "无工单线程正在进行中！！！"
    a = producer.get_instance()
    if a is None:
        try:
            a = producer.new_instance(producer.ip, producer.port, producer.com, producer.baud)
            a.begin()
            result = "无工单启动！！！"
        except Exception as e:
            result = e
    # print(result)


# producer_yuqidong(SampleProducer_yuqidong)

# producer_yqd(SampleProducer_set("192.168.4.124", 51501, None, None))  # 采集设置值  可以一直运行，增量为0
# # producer_yqd(SampleProducer_set(None, None, settings.PORT_PRODUCT_set, settings.BAUD_RATE_PRODUCT))  # 采集设置值  可以一直运行，增量为0
# # producer_yuqidong(SampleProducer_set(None, None, settings.PORT_PRODUCT, settings.BAUD_RATE_PRODUCT))  # 采集设置值  可以一直运行，增量为0
#
# producer_yqd(SampleProducer_actual_values_yqd("192.168.4.124", 52501, None, None))
# # producer_yqd(SampleProducer_actual_values_yqd(None, None, settings.PORT_PRODUCT, settings.BAUD_RATE_PRODUCT))
# # producer_yuqidong(SampleProducer_actual_values_yqd(None, None, settings.PORT_PRODUCT, settings.BAUD_RATE_PRODUCT))

# 模拟wireshark解析数据，不需要端口
producer_yqd(SampleProducer_set_A10("192.168.3.211", 8888, None, None))  # 采集设置值  可以一直运行，增量为0     192.168.3.211好像是机台控制板的ip地址
producer_yqd(SampleProducer_actual_values_yqd_A10("192.168.3.124", 8888, None, None))  # 对应的，192.168.3.124应该是机台主机的Ip地址
